<?php 
class Controller_Admin_Module_News_Newpost extends ypAdminController {
	public function Index() {
		$this->checkLogin();

		$this->loadLanguage('Admin/Module/News/Newpost');

		$this->set('form_action', $this->Link->build('Admin/Module/News/Newpost/Progress'));
		$this->set('ajax_newpost_url', $this->Link->build('Admin/Module/News/Ajax/Getalias', TRUE, array('title' => '')));
		$this->Loader->model('Admin/Module/News/Newpost');
		$this->set('cats', $this->Model_Admin_Module_News_Newpost->loadCat());


		// ===============
		$this->setTemplate('module/Module/News/Newpost.php');
		$this->Document->setTitle($this->_data['title']);
		$this->setTemplateChild(array(
			'Admin/System/Header',
			'Admin/System/Footer'
		));
		$this->Response->setOutput($this->render());
	}

	public function Progress() {
		$this->checkLogin();

		if (!$this->Request->POST['submit']) {
			$this->redirect($this->Link->build('Admin/Module/News/Index', TRUE));
		}

		$this->loadLanguage('Admin/Module/News/Progress');

		$this->Loader->helper('string');
		$this->set('error', '');
		$this->set('title',        htmlspecialchars((string) $this->Request->POST['post_title']));
		$this->set('title_alias',       createAlias($this->_data['title']));
		$this->set('post_id',           (int) $this->Request->POST['post_id']);
		$this->set('post_content',       htmlspecialchars((string) $this->Request->POST['post_content']));
		$this->set('cat',               (int) $this->Request->POST['cat']);
		$this->set('keywork',            htmlspecialchars((string) $this->Request->POST['keywork']));
		$this->set('tags',               htmlspecialchars((string) $this->Request->POST['tags']));

		if (strlen($this->_data['post_title']) < 5 OR strlen($this->_data['post_title']) > 150) {
			$this->set('error', $this->_data['error_title_strlen']);
		} else {
			if ($this->_data['cat'] == 0) {
				$this->set('error', $this->_data['error_chooose_cat']);
			} else {
				$this->Loader->model('Admin/Module/News/Progress');
				$tags = array_map('trim', explode(',', $this->_data['tags']));
				$this->set('user', $this->User->getData());
				$postId = $this->Model_Admin_Module_News_Progress->addPost($this->_data);
				if ($postId) {
					$this->Model_Admin_Module_News_Progress->addTag($tags, $postId);
				}

				if (!$postId) {
					// Add post fail
					$this->set('error', $this->_data['error_add_post_fail']);
				} else {
					// Delete cache 
					$this->cache->delete(array('category_0', 'category_'. $this->_data['cat']));
					$this->set('redirect_to', $this->Link->build('Admin/Module/News/Index'));
					$this->setTemplate('module/Module/News/Progress.php');
					$this->Response->setOutput($this->render());
					return;
				}
			}
		}

		if (!empty($this->_data['error'])) {
			$this->loadLanguage('Admin/Module/News/Newpost');
			$this->set('form_action', $this->Link->build('Admin/Module/News/Newpost/Progress'));
			$this->Loader->model('Admin/Module/News/Newpost');
			$this->set('cats', $this->Model_Admin_Module_News_Newpost->loadCat());
			$this->setTemplate('module/Module/News/Newpost.php');
			$this->Document->setTitle($this->_data['title']);
			$this->setTemplateChild(array(
				'Admin/System/Header',
				'Admin/System/Footer'
			));
			$this->Response->setOutput($this->render());
		}
	}
}